﻿/**********************
 * jiaw
 * 帧处理图片合成参数
**********************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Drawing2D;

namespace VideoFlowStatistics.Class
{

    public class ImageComb
    {
        public ImageComb()
        { }

        /// <summary>
        /// 图片处理
        /// </summary>
        /// <param name="SourceBitmapOne">原图1</param>
        /// <param name="SourceBitmapTwo">原图2</param>
        /// <param name="widthOne">图1截取宽度</param>
        /// <param name="heightOne">图1截取高度</param>
        /// <param name="XOne">图1开始横坐标</param>
        /// <param name="YOne">图1开始纵坐标</param>
        /// <param name="widthTwo">图2截取宽度</param>
        /// <param name="heightTwo">图2截取高度</param>
        /// <param name="XTwo">图2开始横坐标</param>
        /// <param name="YTwo">图2开始纵坐标</param>
        /// <returns></returns>
        public static Bitmap ImageProcess(Bitmap SourceBitmapOne, Bitmap SourceBitmapTwo
            , int widthOne, int heightOne, int XOne, int YOne, int widthTwo, int heightTwo
            , int XTwo, int YTwo, string direction)
        {
            if (SourceBitmapOne == null || SourceBitmapTwo == null)
            {
                return null;
            }
            int width = SourceBitmapOne.Width + SourceBitmapTwo.Width;
            int height = SourceBitmapOne.Height + SourceBitmapTwo.Height;

            Bitmap bitOne = GetPartOfImageRec(SourceBitmapOne, widthOne, heightOne, XOne, YOne);
            Bitmap bitTwo = GetPartOfImageRec(SourceBitmapTwo, widthTwo, heightTwo, XTwo, YTwo);
            //图像合成
            Bitmap bitComb = new Bitmap(width, height);
            Graphics ReducedG = Graphics.FromImage(bitComb);

            ReducedG.DrawImage(bitOne, new Rectangle(0, 0, widthOne, heightOne), new Rectangle(0, 0, widthOne, heightOne), GraphicsUnit.Pixel);
            if (direction == "1")//横向
            {
                ReducedG.DrawImage(bitTwo, new Rectangle(widthOne, 0, widthTwo, heightTwo), new Rectangle(0, 0, widthTwo, heightTwo), GraphicsUnit.Pixel);
            }
            else
            {
                ReducedG.DrawImage(bitTwo, new Rectangle(0, heightOne, widthTwo, heightTwo), new Rectangle(0, 0, widthTwo, heightTwo), GraphicsUnit.Pixel);
            }
            return bitComb;
        }

        /// <summary>
        /// 截取一张图片的制定部分
        /// </summary>
        /// <param name="SourceBitmap">原始图片路径名称</param>
        /// <param name="width">截取图片的宽度</param>
        /// <param name="height">截取图片的高度</param>
        /// <param name="X">开始截取图片的X坐标</param>
        /// <param name="Y">开始截取图片的Y坐标</param>
        /// <returns></returns>
        private static Bitmap GetPartOfImageRec(Bitmap SourceBitmap, int width, int height, int X, int Y)
        {
            Bitmap resultBitmap = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(resultBitmap))
            {
                Rectangle resultRectangle = new Rectangle(0, 0, width, height);
                Rectangle sourceRectangle = new Rectangle(0 + X, 0 + Y, width, height);
                g.DrawImage(SourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
            }
            return resultBitmap;
        }

        /// <summary>
        /// 按大小缩放图片
        /// </summary>
        /// <param name="ResourceImage">原图</param>
        /// <param name="Width">缩放到的宽</param>
        /// <param name="Height">缩放到的高</param>
        /// <returns>Image</returns>
        public static Bitmap ReducedImage(Bitmap ResourceBim, int Width, int Height)
        {
            try
            {
                //Image ReducedImage;
                Bitmap ReducedBip = new Bitmap(Width, Height);
                Graphics ReducedG = Graphics.FromImage(ReducedBip);
                // 插值算法的质量,保证图片质量
                ReducedG.InterpolationMode = InterpolationMode.HighQualityBicubic;
                ReducedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;

                ReducedG.DrawImage(ResourceBim, new Rectangle(0, 0, Width, Height), new Rectangle(0, 0, ResourceBim.Width, ResourceBim.Height), GraphicsUnit.Pixel);
                ReducedG.Dispose();

                return ReducedBip;
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
}
